package com.zygh.lz.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zygh.lz.entity.Patrolrecord;
import com.zygh.lz.entity.vo.Patrolrecords;
import com.zygh.lz.service.PatrolrecordService;
import com.zygh.lz.service.StaffService;
import com.zygh.lz.stapprove.*;
import com.zygh.lz.vo.ResultBean;
import com.zygh.lz.vo.ResultBeans;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "APP巡查模块", description = "AppPatrolrecordCotroller")
@RestController
public class AppPatrolrecordCotroller {
    @Resource
    private PatrolrecordService patrolrecordService;
    @Resource
    private StaffService staffService;


    @ApiOperation(value = "根据登录用户负责道路列表")
    @GetMapping("selectByStaffId")
    @ViLog(logType = "1", module = "根据登录用户负责道路列表")
    public ResultBean selectByStaffId(Integer staffId) {
        return patrolrecordService.selectByStaffId(staffId);
    }

    //@ApiOperation(value = "新增巡查记录")
    //@PostMapping("addPatrolrecord")
    //@ViLog(logType = "1", module = "新增巡查记录")
//    public OutBean addPatrolrecord(Patrolrecord patrolrecord, @RequestBody(required = false) InBean inBean) {
//        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
//        patrolrecord.setSysStaffId(Integer.valueOf(jsonObject.getString("sysStaffId")));
//        patrolrecord.setSignnum(jsonObject.getString("signNum"));
//        patrolrecord.setPatrolRecordBegintime(new Date());
//        patrolrecord.setRoadName(jsonObject.getString("roadName"));
//        patrolrecord.setPatrolRecordDetail(jsonObject.getString("patrolRecordDetail"));
//        patrolrecord.setPatrolRecordGps(jsonObject.getString("patrolRecordGps"));
//        ResultBeans resultBeans = patrolrecordService.addPatrolrecord(patrolrecord);
//        return OutBeanUtil.resSuc(resultBeans, inBean);
//    }

    @ApiOperation(value = "修改巡查记录")
    @PostMapping("updatePatrolrecord")
    @ViLog(logType = "1", module = "修改巡查记录")
    public OutBean updatePatrolrecord(Patrolrecord patrolrecord, @RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        patrolrecord.setSysPatrolRecordId(Integer.valueOf(jsonObject.getString("sysPatrolRecordId")));
        patrolrecord.setSysStaffId(Integer.valueOf(jsonObject.getString("sysStaffId")));
        patrolrecord.setPatrolRecordEndtime(new Date());
        patrolrecord.setPatrolRecordVideo(jsonObject.getString("patrolRecordVideo"));
        patrolrecord.setPatrolRecordPicture(jsonObject.getString("patrolRecordPicture"));
        patrolrecord.setPatrolRecordDetail(jsonObject.getString("patrolRecordDetail"));
        ResultBeans resultBeans = patrolrecordService.updatePatrolrecord(patrolrecord);
        return OutBeanUtil.resSuc(resultBeans, inBean);
    }


    @PostMapping("selectAllPatrolrecordById")
    @ViLog(logType = "1", module = "APP巡查记录")
    @ApiOperation(value = "APP巡查记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", dataType = "Integer", required = true, paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页数量", dataType = "Integer", required = true, paramType = "query")

    })
    public OutBean selectAllPatrolrecordById(@RequestParam(name = "current", defaultValue = "1") Integer current,
                                             @RequestParam(name = "size", defaultValue = "10") Integer size,
                                             @RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        Integer id = Integer.parseInt(jsonObject.getString("SysStaffId"));
        current = Integer.parseInt(jsonObject.getString("current"));
        size = Integer.parseInt(jsonObject.getString("size"));
        String time = jsonObject.getString("Time");
        ResultBeans resultBeans = patrolrecordService.selectAllPatrolrecordById(id,time, current, size);
        return OutBeanUtil.resSuc(resultBeans, inBean);
    }


    @GetMapping("selectinfoByid")
    @ViLog(logType = "1", module = "根据个人id查询该人当天是否签到")
    public ResultBean selectinfoByid(Integer id) {
        return patrolrecordService.selectinfoByid(id);
    }

    @ApiOperation("返回在线民警GPS")
    @GetMapping("selectStaffZx")
    @ViLog(logType = "1", module = "返回在线民警GPS")
    public ResultBean selectStaffZx(Integer id) {
        return staffService.selectStaffZx(id);
    }

    @ApiOperation("统计夜间各个岗位应到实到人")
    @GetMapping("yXtypeSum")
    public List<Map> yXtypeSum(String battalion) {
        return patrolrecordService.yXtypeSum(battalion);
    }

    @ApiOperation("根据民警id查询该民警所负责的路段里程，上班时长")
    @GetMapping("selectStaffXareaByid")
    public ResultBean selectStaffXareaByid(Integer id) throws Exception {
        return staffService.selectStaffXareaByid(id);
    }


}
